//
//  MapViewController.m
//  iPath
//
//  Created by Janidu Wanigasuriya on 10/30/12.
//  Copyright (c) 2012 99XTechnology. All rights reserved.
//

#import "MapViewController.h"


@interface MapViewController ()
@property (nonatomic)LocationService *locationService;
@property (nonatomic)NSDate *startDate;
@property (nonatomic)NSString *startDateString;
@end

@implementation MapViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.locationService = [[LocationService alloc] init];
    self.trackWorker = [[TrackWorker alloc] initWithMapView:self.mapView];
    
    DBWrapper *dbWrapper = [[DBWrapper alloc] init];
    NSMutableArray* poiList = [dbWrapper getPOIList];
    
    self.poiFinder = [[POIFinderWorker alloc] initWithPOIList:poiList];
    self.poiFinder.delegate = self;
    
    [self addPOIAnnotations:poiList];
    
    isRecording = false;
}

-(void)addPOIAnnotations:(NSMutableArray *)pois{
    
    for (int i=0; i<pois.count; i++) {
        
        POIObject *obj = [pois objectAtIndex:i];
        PointObject *point = obj.point;
        
        double lat = [point.latitude doubleValue];
        double longi = [point.longitude doubleValue];
        
        CLLocationCoordinate2D location = CLLocationCoordinate2DMake(lat, longi);
        
        // Add the annotation to our map view
        MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc]
                                            initWithTitle:obj.description andCoordinate:location];
        [self.mapView addAnnotation:newAnnotation];
    }
}

-(void)nearByPOI:(NSArray *)nearByPOI{
    
    for (int i = 0; i < nearByPOI.count; i++) {
        POIObject *poiObj=(POIObject*)[nearByPOI objectAtIndex:i];
        NSString *message = [NSString stringWithFormat:@"POI %@ with distance %@",
                             poiObj.description , poiObj.distanceTocurrentLocation];
        [self showToastWithMsg:message forDuration:iToastDurationNormal];
    }
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    [self zoomMapToCoordinate:userLocation.coordinate];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if (!self.trackWorker.crumbPathView) {
        self.trackWorker.crumbPathView = [[CrumbPathView alloc] initWithOverlay:overlay];
    }
    return self.trackWorker.crumbPathView;
}

- (IBAction)clickSaveTrack:(id)sender {
    
    if([self markEmptyTextField:self.trackFrom :YES] && [self markEmptyTextField:self.trackTo :YES]){
        MoreInfo *moreInfo = [[MoreInfo alloc]
                              initWithDistance:self.distanceLabel.text trackFrom:self.trackFrom.text trackTo:self.trackTo.text
                              duration:self.durationLabel.text startTime:self.startDateString];
        
        DBWrapper *dbWrapper = [[DBWrapper alloc] init];
        
        [dbWrapper insertTrackWithLocations:self.trackWorker.locationList moreInfo:moreInfo];
        
        UIAlertView *savedAlert = [[UIAlertView alloc] initWithTitle:@"Track Saved" message:nil
                                                            delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [savedAlert show];
        self.trackSaveView.hidden = YES;
        
        self.trackFrom.text=@"";
        self.trackTo.text=@"";
    }
}

-(void)showTrackSaveView{
    
    
    float totalDistance = [self.trackWorker.crumbs getTotatlDistance];
   
    
    if(totalDistance > 0.0f ){
        
        
        self.trackSaveView.hidden = NO;
        self.trackSaveView.alpha = 0;
        
        [UIView animateWithDuration:0.1 animations:^{self.trackSaveView.alpha = 1.0;}];
        self.trackSaveView.layer.transform = CATransform3DMakeScale(0.5, 0.5, 1.0);
        
        CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        bounceAnimation.values = [NSArray arrayWithObjects:
                                  [NSNumber numberWithFloat:0.5],
                                  [NSNumber numberWithFloat:1.1],
                                  [NSNumber numberWithFloat:0.8],
                                  [NSNumber numberWithFloat:1.0], nil];
        
        bounceAnimation.duration = 0.3;
        bounceAnimation.removedOnCompletion = NO;
        [self.trackSaveView.layer addAnimation:bounceAnimation forKey:@"bounce"];
        self.trackSaveView.layer.transform = CATransform3DIdentity;
        
        //Set total distance
        double totalDistance = [self.trackWorker.crumbs getTotatlDistance];
        self.distanceLabel.text = [NSString stringWithFormat:@"%.2f", totalDistance/1000];
        
        //Set Start Date
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"EEE, hh:mm a"];
        self.startDateString = [dateFormatter stringFromDate:self.startDate];
        
        //Set Duration
        NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:self.startDate];
        NSString *duration = [NSString stringWithFormat:@"%02li:%02li",
                              lround(floor(time / 3600.)) % 100,
                              lround(floor(time / 60.)) % 60];
        
        self.durationLabel.text = duration;
        
    }
}


- (IBAction)clickCurrentLocation:(id)sender {
    
    if(isShowingSavedTrack){
        self.recordPathBtn.enabled = YES;
        [self.trackWorker clearOverlay];
    }
    
    CLLocationCoordinate2D currentLocation = [[[self.mapView userLocation] location] coordinate];
    [self.mapView setCenterCoordinate:currentLocation];
}

- (IBAction)clickRecordPath:(id)sender {
    
    isRecording =!isRecording ;
    
    if(isRecording){
        
        [self.trackWorker track];
        [self.poiFinder findNearByPOI];
        
        self.recordPathBtn.title = @"Stop";
        [self toggleRecordIndicator];
        self.trackSaveView.hidden = YES;
        self.currentLocationBtn.enabled=NO;
        
        // Save track recording start time
        self.startDate = [NSDate date];
    }
    else{
        [self showTrackSaveView];
        //self.currentLocationBtn.enabled=YES;
        [self.trackWorker stop];
        [self.trackWorker clearOverlay];
        
        [self.poiFinder stop];
        
        // change stop button text to record
        self.recordPathBtn.title = @"Record";
        [self toggleRecordIndicator];
        
        self.recordingIndicatorImage.hidden = YES;
        self.currentLocationBtn.enabled = YES;
    }
}

-(void)drawTrack:(TrackDetail *)track{
    
    [self.trackWorker drawTrack:track];
    
    isShowingSavedTrack = YES;
    
    self.recordPathBtn.enabled = NO;
    self.recordPathBtn.title = @"Record";
    
    isRecording = NO;
    [self toggleRecordIndicator];
}


//Zoom map to a given coordinate
-(void)zoomMapToCoordinate:(CLLocationCoordinate2D) coordinate{
    MKCoordinateRegion region =
    MKCoordinateRegionMakeWithDistance(coordinate,
                                       2000,
                                       2000);
    [self.mapView setRegion:region animated:YES];
}
- (void)viewDidUnload {
    [self setCurrentLocationBtn:nil];
    [self setRecordPathBtn:nil];
    [self setRecordingIndicatorImage:nil];
    [self setBtnSaveTrack:nil];
    [self setTrackFrom:nil];
    [self setTrackFrom:nil];
    [self setTrackTo:nil];
    [self setTrackSaveView:nil];
    [self setTrackFrom:nil];
    [self setTrackTo:nil];
    [self setTrackFrom:nil];
    [self setTrackTo:nil];
    [self setDistanceLabel:nil];
    [self setDurationLabel:nil];
    [self setTimeLabel:nil];
    [self setTrackFrom:nil];
    [self setTrackTo:nil];
    [self setBtnClose:nil];
    [self setBtnSaveTrack:nil];
    [super viewDidUnload];
}

-(void)toggleRecordIndicator{
    
    if (!isRecording) {
        self.recordingIndicatorImage.hidden = YES;
        self.recordingIndicatorImage.alpha  = 0.0;
        return;
    }else{
        self.recordingIndicatorImage.hidden = NO;
        self.recordingIndicatorImage.alpha  = 1.0;
    }
    
    [UIView animateWithDuration: 2.0
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         self.recordingIndicatorImage.alpha = !self.recordingIndicatorImage.alpha;
                     }
                     completion:^(BOOL finished) {
                         [self toggleRecordIndicator];
                     }];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (IBAction)clickClose:(id)sender {
    self.trackSaveView.hidden = YES;
}

-(BOOL)markEmptyTextField:(UITextField *)textField :(BOOL)colorTheField{
    
    if(textField){
        
        
        if(textField.text.length == 0){
            if (colorTheField) {
                [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
                
                shakeDirection = 1;
                numberOfShakes = 0;
                [self shake:textField];
            }
            return NO;
        }else{
            //Sanitize if user as pipe characherters
            textField.text = [textField.text stringByReplacingOccurrencesOfString:@"|" withString:@""];
        }
    }
    return YES;
}

-(void)shake:(UIView *)view{
    
    [UIView animateWithDuration:0.07 animations:^
     {
         view.transform = CGAffineTransformMakeTranslation(5*shakeDirection, 0);
     }
                     completion:^(BOOL finished)
     {
         if(numberOfShakes >= 15)
         {
             view.transform = CGAffineTransformIdentity;
             return;
         }
         numberOfShakes++;
         shakeDirection = shakeDirection * -1;
         [self shake:view];
     }];
}

- (void) showToastWithMsg:(NSString *) message forDuration:(int) duration{
    if(self.genericToastMsg == nil) {
        self.genericToastMsg = [iToast makeText:message withTextSize:[UIFont systemFontOfSize:16]];
    }
    [self.genericToastMsg scheduleToast:message forDuration:duration];
}
@end
